﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;


namespace WebUtility
{
    public class Common
    {

        public static string AmountInWords(decimal amount)
        {
            int ONECRORE = 10000000;
            int ONELACK = 100000;
            int ONETHOUSAND = 1000;
            int ONEHUNDRED = 100;
            StringBuilder sb = new StringBuilder();
            //Million 1,000,000
            //Lack 1,00,000
            //Thousand 1,000
            //Hundred 100
            int intAmount = (int)amount;

            int crore = intAmount / ONECRORE;
            intAmount = intAmount % ONECRORE;

            if (crore > 0)
            {
                sb.AppendFormat("{0} Crore ", NumberToString(crore));
            }

            int lack = intAmount / ONELACK;
            intAmount = intAmount % ONELACK;

            if (lack > 0)
            {
                sb.AppendFormat("{0} Lack ", NumberToString(lack));
            }

            int thousand = intAmount / ONETHOUSAND;
            intAmount = intAmount % ONETHOUSAND;

            if (thousand > 0)
            {
                sb.AppendFormat("{0} Thousand ", NumberToString(thousand));
            }

            int hundred = intAmount / ONEHUNDRED;
            intAmount = intAmount % ONEHUNDRED;

            if (hundred > 0)
            {
                sb.AppendFormat("{0} Hundred ", NumberToString(hundred));
            }

            sb.AppendFormat("{0} Rupees And ", NumberToString(intAmount));

            string strAmount = amount.ToString();
            int decAmount = Convert.ToInt32(strAmount.Substring(strAmount.IndexOf(".") + 1));
            if (decAmount > 0)
            {
                sb.AppendFormat("{0} Paisa ", NumberToString(decAmount));
            }
            else
            {
                sb.Append("Zero Paisa ");
            }
            return sb.ToString();
        }

        private static string NumberToString(int number)
        {
            if (number > 19)
            {
                string strNumber = string.Empty;
                int tens = number / 10;
                strNumber = Tens(tens);
                number = number % 10;
                strNumber += " " + OneToNinteen(number);
                return strNumber;
            }
            else
            {
                return OneToNinteen(number);
            }
        }

        private static string OneToNinteen(int number)
        {
            string strNumber = string.Empty;
            switch (number)
            {
                case 1: strNumber = "One"; break;
                case 2: strNumber = "Two"; break;
                case 3: strNumber = "Three"; break;
                case 4: strNumber = "Four"; break;
                case 5: strNumber = "Five"; break;
                case 6: strNumber = "Six"; break;
                case 7: strNumber = "Seven"; break;
                case 8: strNumber = "Eight"; break;
                case 9: strNumber = "Nine"; break;
                case 10: strNumber = "Ten"; break;
                case 11: strNumber = "Eleven"; break;
                case 12: strNumber = "Twelve"; break;
                case 13: strNumber = "Thirteen"; break;
                case 14: strNumber = "Fourteen"; break;
                case 15: strNumber = "Fifteen"; break;
                case 16: strNumber = "Sixteen"; break;
                case 17: strNumber = "Seventeen"; break;
                case 18: strNumber = "Eighteen"; break;
                case 19: strNumber = "Ninteen"; break;
            }
            return strNumber;
        }

        private static string Tens(int number)
        {
            string strNumber = string.Empty;
            switch (number)
            {
                case 2: strNumber = "Twenty"; break;
                case 3: strNumber = "Thirty"; break;
                case 4: strNumber = "Forty"; break;
                case 5: strNumber = "Fifty"; break;
                case 6: strNumber = "Sixty"; break;
                case 7: strNumber = "Seventy"; break;
                case 8: strNumber = "Eighty"; break;
                case 9: strNumber = "Ninty"; break;
            }
            return strNumber;
        }

    }
}